package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PositionSalaryChange;
import com.amsp.oa.oademo.mapper.PositionSalaryChangeMapper;
import com.amsp.oa.oademo.service.PositionSalaryChangeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class PositionSalaryChangeServiceImpl extends ServiceImpl<PositionSalaryChangeMapper, PositionSalaryChange>
        implements PositionSalaryChangeService {

    @Override
    public IPage<PositionSalaryChange> getPage(Integer current, Integer size, String employeeNo, String employeeName,
            String originalDepartment, String newDepartment, String changeType, String startDate, String endDate) {
        LambdaQueryWrapper<PositionSalaryChange> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        wrapper.like(StringUtils.isNotBlank(employeeNo), PositionSalaryChange::getEmployeeNo, employeeNo)
                .like(StringUtils.isNotBlank(employeeName), PositionSalaryChange::getEmployeeName, employeeName)
                .like(StringUtils.isNotBlank(originalDepartment), PositionSalaryChange::getOriginalDepartment,
                        originalDepartment)
                .like(StringUtils.isNotBlank(newDepartment), PositionSalaryChange::getNewDepartment, newDepartment);

        // 处理变更类型
        if (StringUtils.isNotBlank(changeType)) {
            switch (changeType) {
                case "formalHire":
                    wrapper.eq(PositionSalaryChange::getIsFormalHire, true);
                    break;
                case "terminateLabor":
                    wrapper.eq(PositionSalaryChange::getIsTerminateLabor, true);
                    break;
                case "extendProbation":
                    wrapper.eq(PositionSalaryChange::getIsExtendProbation, true);
                    break;
                case "changePosition":
                    wrapper.eq(PositionSalaryChange::getIsChangePosition, true);
                    break;
                case "changeSalary":
                    wrapper.eq(PositionSalaryChange::getIsChangeSalary, true);
                    break;
            }
        }

        // 处理申请时间范围
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            wrapper.between(PositionSalaryChange::getApplyDate, startDate, endDate + " 23:59:59");
        }

        // 按申请时间倒序排序
        wrapper.orderByDesc(PositionSalaryChange::getApplyDate);

        return page(new Page<>(current, size), wrapper);
    }
}